﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoastalClimbing.Code;
using System.Data;

namespace CoastalClimbing.Auth
{
    public partial class HomePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                HandlePostback(IsPostBack);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string message = "Blurb successfully published.";

            try
            {
                LatestBlurb blurb = null;

                if (ViewState["BLURB"] == null)
                {
                    blurb = new LatestBlurb(txtTitle.Text, txtMessage.Text, new Guid(ddlTitleStyle.SelectedValue.ToString()), new Guid(ddlMessageStyle.SelectedValue.ToString()));
                }
                else
                {
                    blurb = (LatestBlurb)ViewState["BLURB"];
                }

                DAL.UpdateLatestBlurb(blurb);
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            lblMessage.Text = message;
            lblMessage.Visible = true;
        }

        private void HandlePostback(bool isPostback)        
        {
            LatestBlurb blurb = null;

            if (ViewState["BLURB"] == null)
            {
                blurb = DAL.GetLatestBlurb();
                ViewState["BLURB"] = blurb;
            }
            else
            {
                blurb = (LatestBlurb)ViewState["BLURB"];
            }

            if(isPostback)
            {
                if(blurb == null)
                {
                    blurb = new LatestBlurb();
                }

                blurb.Message = txtMessage.Text;
                blurb.Title = txtTitle.Text;
                blurb.MessageStyleID = new Guid(ddlMessageStyle.SelectedValue);
                blurb.TitleStyleID = new Guid(ddlTitleStyle.SelectedValue);

                ViewState["BLURB"] = blurb;
            }
            else
            {
                DataTable dt = DAL.GetStyles();
                BindDDL(ddlMessageStyle, dt);
                BindDDL(ddlTitleStyle, dt);
            }
            
            if(blurb != null)
            {
                txtTitle.Text = blurb.Title;
                txtMessage.Text = blurb.Message;
                ddlMessageStyle.SelectedValue = blurb.MessageStyleID.ToString();
                ddlTitleStyle.SelectedValue = blurb.TitleStyleID.ToString();

                litTitle.Text = "<span class='" + ddlTitleStyle.SelectedItem.Text + "'>" + blurb.Title + "</span>";
                litMessage.Text = "<span class='" + ddlMessageStyle.SelectedItem.Text + "'>" + blurb.Message + "</span>";
            }
        }

        private void BindDDL(DropDownList ddl, DataTable dt)
        {
            ddl.DataSource = dt;
            ddl.DataTextField = "STYLE";
            ddl.DataValueField = "ID";
            ddl.DataBind();
        }

        protected void txtTitle_TextChanged(object sender, EventArgs e)
        {
            string eArg = Request.Params["__EVENTARGUMENT"];

            if (eArg == "txtTitleKeyPress")
            {
                HandlePostback(true);
                txtTitle.Focus();
            }
        }

        protected void txtMessage_TextChanged(object sender, EventArgs e)
        {
            string eArg = Request.Params["__EVENTARGUMENT"];

            if (eArg == "txtMessageKeyPress")
            {
                HandlePostback(true);
                txtMessage.Focus();
            }
        }

        protected void ddlTitleStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            HandlePostback(true);
        }

        protected void ddlMessageStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            HandlePostback(true);
        }
    }
}